/*
 * @Author: zhangguliu1234 984556209@.com
 * @Date: 2023-12-25 11:37:02
 * @LastEditors: zhangguliu1234 984556209@.com
 * @LastEditTime: 2024-03-07 12:00:21
 * @FilePath: \BED2\components\BedSpread.h
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
 */
#include <stdio.h>
// #include "mqtt_client.h"
// #include "my_wss.h"

#define Bed_Storage "Bed_Storage"

typedef struct
{
    int state; // 功能状态位
    // left
    int left_uptemp;   // 左上温度值
    int left_downtemp; // 左下温度值
    int left_VibFreq;  // 左律动频率
    int left_VibInt;   // 左律动强度
    int left_HR;       // 左心率
    int left_RR;       // 左呼吸率
    int left_Motion;   // 左体动状态
    // right
    int right_uptemp;   // 右上温度值
    int right_downtemp; // 右下温度值
    int right_VibFreq;  // 右律动频率
    int right_VibInt;   // 右律动强度
    int right_HR;       // 右心率
    int right_RR;       // 右呼吸率
    int right_Motion;   // 右体动状态
    // 校验值
    char check;
    // 床内湿度值
    char BedWet;
    // 新版固件
    int bed_key;
    int left_mode;
    int right_mode;
    int left_settemp;
    int right_settemp;
    int left_nowtemp;
    int right_nowtemp;
    int left_time;
    int right_time;
    int LED;
    int left_isonbed;
    int right_isonbed;
} BedSpread;

typedef struct
{
    time_t timestamp;
    char deviceType[20];
    char deviceId[14];
    char type[12];
    char once_time[13];
    char week_time[13];
    char date[12];
    char week[6];
    int left_upkey;
    int right_upkey;
    int left_downkey;
    int right_downkey;
    int cost;
    int left_uptemp;
    int right_uptemp;
    int left_downtemp;
    int right_downtemp;
    int left_VibFreq;
    int left_VibInt;
    int right_VibFreq;
    int right_VibInt;
    // 新版固件
    int bed_key;
    int left_mode;
    int right_mode;
    int left_settemp;
    int right_settemp;
    int left_nowtemp;
    int right_nowtemp;
    int left_time;
    int right_time;
    int LED;
    int left_isonbed;
    int right_isonbed;
} BedSyncPolicy;

typedef struct
{
    int lefttime_set;  // 左区是否设置定时
    int righttime_set; // 右区是否设置定时
    time_t left_starttime;
    time_t left_endtime;
    time_t right_starttime;
    time_t right_endtime;
} BedControlTime;

typedef struct esp_mqtt_client *esp_mqtt_client_handle_t;
typedef struct cJSON cJSON;
typedef struct CJsonStruct myCJson;

extern volatile SemaphoreHandle_t xSemaphore;
extern volatile SemaphoreHandle_t xSemaphore2;
extern BedSpread Bed_Test;
extern BedSpread Bed_Read;
extern BedSpread Bed_Write;
extern BedControlTime Bed_Time;
extern BedSyncPolicy Bed_Sync;
extern TaskHandle_t BedSyncOnceHandle;
extern TaskHandle_t BedChangeHandle;
extern TaskHandle_t BedSyncWeekHandle;
extern TaskHandle_t BedRXTaskHandle;
extern char Bed_Control_State[8];
extern char Bed_Write_State[8];
extern char Bed_Read_State[8];
extern char Bed_Test_State[8];
extern char Bed_SyncPolicy_State[8]; // 同步策略操作标志位  0~3：开关，4~7：温度，8~11：律动 12:消费状态
extern char BedSyncOnce_flag;
extern char BedSyncWeek_flag;
extern char BedChange_flag;
extern volatile int bed_rx;
extern volatile int bed_tx;
extern volatile int mqtt_read;
extern int bed_test;
extern int policy_tx;
extern int WifiTest;
void BedSpread_uart_init(void);
void BED_send(const int port, const char *str, uint8_t length);
// void BedControl(esp_mqtt_client_handle_t *client, char *topic);
// void Bed_ReadTask(void *arg);
void hexToBin(char hexValue, char *binaryArray);
int BinarrayToInt(char binaryArray[], int length);
int Bed_ReadState(esp_mqtt_client_handle_t *client, char *topic);
void Publish_Bed_State(esp_mqtt_client_handle_t *client, char *topic, uint8_t *cbdata, char *hr_rr_body);
void Publish_Reply_Message(esp_mqtt_client_handle_t client, char *topic, bool state);
void GetBedJson_Control_State(myCJson *data, cJSON *item, int type);
int Bed_SyncPolicy_Save(myCJson *data);
// 同步策略任务开启的前置条件，用于主函数中，当刚开机时判断是否要执行策略
int Bed_SyncPolicy_TaskKey(void);
// 执行一次策略任务
void Bed_SyncPolicy_OneTime_Task(void *arg);
void Bed_SyncPolicy_OneTimeWeek_Task(void *arg);
void iterate_through_nvs(void);
void Bed_Nvs_Erase_All(void);
void bed_rx_task(void *arg);
void bed_rxdata_parse(uint8_t *data, int rxBytes);
void bed_control_task(void *arg);
void bed_change_task(void *arg);
int build_bed_cmd(char *write_cmd, int sign);
int Bed_Change_TaskKey(void);
void Publish_WifiTest_Result(esp_mqtt_client_handle_t client, char *topic, bool state);
void bed_controltime_task(void *arg);